package com.winit.schedule.job.tracker;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.winit.schedule.core.conf.Configuration;
import com.winit.schedule.core.event.Dispatcher;
import com.winit.schedule.core.repository.entity.TaskTrackerEntity;
import com.winit.schedule.core.utils.Clock;
import com.winit.schedule.job.tracker.monitor.TaskTrackerMonitor;
import com.winit.schedule.job.tracker.monitor.ZKServiceMonitor;
import com.winit.schedule.job.tracker.remoting.RemotingService;
import com.winit.schedule.job.tracker.repository.ConsoleDataManager;
import com.winit.schedule.job.tracker.repository.ScheduleDataManager;
import com.winit.schedule.job.tracker.schedule.TaskScheduleManager;
import com.winit.schedule.job.tracker.sms.SmsSender;
import com.winit.schedule.job.tracker.util.MailHelper;

public class JobTrackerContextImpl implements JobTrackerContext {

    private volatile transient Map<String, TaskTrackerEntity> taskTrackers             = new ConcurrentHashMap<String, TaskTrackerEntity>();

    private volatile transient Set<TaskTrackerEntity>         disconnectTaskTrackerSet = Collections.synchronizedSet(new HashSet<TaskTrackerEntity>());

    private Configuration                                  conf;

    private Dispatcher                                     dispatcher;

    private RemotingService                                remotingService;

    private ScheduleDataManager                            scheduleDataManager;

    private ConsoleDataManager                             consoleDataManager;

    private TaskTrackerMonitor                             taskTrackerMonitor;

    private TaskScheduleManager                            taskScheduleManager;

    private long                                           jobTrackerStartupTime;

    private JobTracker                                     jobTracker;

    private MailHelper                                     mailHelper;

    private ZKServiceMonitor                               zkService;

    private Dispatcher                                     dataDispatcher;

    private SmsSender                                      smsSender;

    public Dispatcher getDispatcher() {
        return dispatcher;
    }

    public void setDispatcher(Dispatcher dispatcher) {
        this.dispatcher = dispatcher;
    }

    public RemotingService getRemotingService() {
        return remotingService;
    }

    public void setRemotingService(RemotingService remotingService) {
        this.remotingService = remotingService;
    }

    public ScheduleDataManager getScheduleDataManager() {
        return scheduleDataManager;
    }

    public void setScheduleDataManager(ScheduleDataManager scheduleDataManager) {
        this.scheduleDataManager = scheduleDataManager;
    }

    public Map<String, TaskTrackerEntity> getTaskTrackers() {
        return taskTrackers;
    }

    public Set<TaskTrackerEntity> getDisconnectTaskTrackerSet() {
        return disconnectTaskTrackerSet;
    }

    public TaskTrackerMonitor getTaskTrackerMonitor() {
        return taskTrackerMonitor;
    }

    public void setTaskTrackerMonitor(TaskTrackerMonitor taskTrackerMonitor) {
        this.taskTrackerMonitor = taskTrackerMonitor;
    }

    public TaskScheduleManager getTaskScheduleManager() {
        return taskScheduleManager;
    }

    public void setTaskScheduleManager(TaskScheduleManager taskScheduleManager) {
        this.taskScheduleManager = taskScheduleManager;
    }

    public long getJobTrackerStartupTime() {
        return jobTrackerStartupTime;
    }

    public void setJobTrackerStartupTime(long jobTrackerStartupTime) {
        this.jobTrackerStartupTime = jobTrackerStartupTime;
    }

    public JobTracker getJobTracker() {
        return jobTracker;
    }

    public void setJobTracker(JobTracker jobTracker) {
        this.jobTracker = jobTracker;
    }

    @Override
    public String getJobTrackerUuid() {
        return this.jobTracker.getUuid();
    }

    @Override
    public boolean allowAssignedTasks() {
        return Clock.getSystemTime() > (taskTrackerMonitor.getHeartbeatTimeout() + jobTrackerStartupTime);
    }

    public void registerTaskTracker(TaskTrackerEntity taskTracker) {
        this.taskTrackers.put(taskTracker.getTaskTrackerUuid(), taskTracker);

    }

    public void unregisterTaskTracker(TaskTrackerEntity taskTracker) {
        this.taskTrackers.remove(taskTracker.getTaskTrackerUuid());
    }

    public ConsoleDataManager getConsoleDataManager() {
        return consoleDataManager;
    }

    public void setConsoleDataManager(ConsoleDataManager consoleDataManager) {
        this.consoleDataManager = consoleDataManager;
    }

    public MailHelper getMailHelper() {
        return mailHelper;
    }

    public void setMailHelper(MailHelper mailHelper) {
        this.mailHelper = mailHelper;
    }

    public Configuration getConf() {
        return conf;
    }

    public void setConf(Configuration conf) {
        this.conf = conf;
    }

    public ZKServiceMonitor getZkService() {
        return zkService;
    }

    public void setSmsSender(SmsSender smsSender) {
        this.smsSender = smsSender;
    }

    @Override
    public SmsSender getSmsSender() {
        return smsSender;
    }

    public void setZkService(ZKServiceMonitor zkService) {
        this.zkService = zkService;
    }

    public Dispatcher getDataDispatcher() {
        return dataDispatcher;
    }

    public void setDataDispatcher(Dispatcher dataDispatcher) {
        this.dataDispatcher = dataDispatcher;
    }
}
